var t = getApp(), map = new (require("../../utils/qqmap-wx-jssdk.min.js"))({
  key: "KJXBZ-V2PW6-N34SN-ENJTN-XP62S-ADFBT"
})
var eventChannel
Page({
  data: {
    lbs:{
      latitude: 34.257784,
      longitude: 117.244720,
    },
    now:{
      title:"",
      desc:""
    },
    list:[],
    selectXiaoquId:0
  },
  onLoad:function(option){
    // console.log(option.query)
    eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('acceptDataFromOpenedPage', {data: 'test'});
    var that = this;
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function(data) {
     // console.log('eeeeeeeee',data)
      that.setData({
        selectXiaoquId:data.selectXiaoquId
      })
    })
  },
  onShow: function (e) {
    this.mapCtx = wx.createMapContext('myMap');
    this.moveLocation();
    var latitude = this.data.lbs.latitude;
    var longitude = this.data.lbs.longitude;
    
    this.analysisTap(latitude, longitude)
    this.getXiaoqu(latitude, longitude);
  },
  selectXiaoqu:function(e){
    console.log(e);
    var id = e.currentTarget.id;
    var name = e.currentTarget.dataset.name;
    this.setData({
      selectXiaoquId:id
    })
    eventChannel.emit('selectXiaoquEvent', {selectXiaoquId:id,selectXiaoquName:name });
    wx.navigateBack({
      delta: 1
    })
  },
  getCenterLocation: function () {
    this.mapCtx.getCenterLocation({
      success: function(res){
        console.log(res.longitude)
        console.log(res.latitude)
      }
    })
  },
  moveLocation:function(){
    this.mapCtx.moveToLocation()
  },
  mapchangeTap: function(e) {
    console.log(e)
    
    if(e.type == 'end'){
      // 中心点
      let that = this;

      // this.mapCtx = wx.createMapContext('myMap');
      this.mapCtx.getCenterLocation({
        success(res) {
          console.log(res)
          const latitude = res.latitude
          const longitude = res.longitude
          that.analysisTap(latitude, longitude);
          that.getXiaoqu(latitude, longitude);
        }
      })
      




    }
    
  },
  // 地址逆解析
  analysisTap: function(lat, lng) {
    let that = this;
    map.reverseGeocoder({
      location: {
        latitude: lat,
        longitude: lng
      },
      success: function(res) {
        console.log(res)
        let province = res.result.address_component.province
        let city = res.result.address_component.city
        let recommend = res.result.formatted_addresses.recommend
        let local = province + city + recommend
        console.log(recommend,local)
        that.setData({
          now: {
            title:res.result.address,
            desc:recommend
          },
         
        })
      },fail: function(error) {
        console.error(error);
      },
    })
  },
  //获取小区
  getXiaoqu:function(latitude,longitude){
    var token = wx.getStorageSync("token")
    var that = this
    wx.request({
      url: t.apiUrl("user/address/getXiaoquByLbs"),
      data: {
        latitude: latitude,
        longitude: longitude,
        
      },
      method: "post",
      header: {
        "Content-Type": "application/json",
        "X-ECTouch-Authorization": token
      },
      success: function (t) {
       
        var data = t.data.data;
        var list = [];
        data.forEach((item,key)=>{
          item.distance = parseInt(item.distance * 1000)
          list.push(item);
        })
        that.setData({
          list:list
        })
      }
    });
  },
  getCenterLocation: function () {
    this.mapCtx.getCenterLocation({
      success: function(res){
        console.log(res.longitude)
        console.log(res.latitude)
      }
    })
  },
  moveToLocation: function () {
    this.mapCtx.moveToLocation()
  },
  translateMarker: function() {
    this.mapCtx.translateMarker({
      markerId: 1,
      autoRotate: true,
      duration: 1000,
      destination: {
        latitude:23.10229,
        longitude:113.3345211,
      },
      animationEnd() {
        console.log('animation end')
      }
    })
  },
  includePoints: function() {
    this.mapCtx.includePoints({
      padding: [10],
      points: [{
        latitude:23.10229,
        longitude:113.3345211,
      }, {
        latitude:23.00229,
        longitude:113.3345211,
      }]
    })
  }
})
